﻿
using System;
using System.ServiceModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnterpriseServices.DalEf;

namespace EnterpriseServices.Service
{
    /// <summary>
    /// 
    /// </summary>
    public class RoleServiceRemoting : RoleServiceBase, IRoleManagerRemoting
    {
        #region IRoleManagerRemoting Members
        /// <summary>
        /// Adds the specified userNames to the specified role.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userNames">The userName names.</param>
        /// <param name="role">The role.</param>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = true)]
        new public void AddUsersToRole(string applicationName, string[] userNames, string role)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
            if (userNames == null || userNames.Length == 0)
                throw new FaultException("userNames is null or empty.");
            if (String.IsNullOrEmpty(role))
                throw new FaultException("role is null or empty.");
            
            try
            {
                base.AddUsersToRole(applicationName, userNames, role);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        /// <summary>
        /// Adds the specified userNames to the specified roles.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userNames">The userName names.</param>
        /// <param name="roles">The roles.</param>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = true)]
        new public void AddUsersToRoles(string applicationName, string[] userNames, string[] roles)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
            if (userNames == null || userNames.Length == 0)
                throw new FaultException("userNames is null or empty.");
            if (roles == null || roles.Length == 0)
                throw new FaultException("roles is null or empty.");
            
            try
            {
                base.AddUsersToRoles(applicationName, userNames, roles);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        /// <summary>
        /// Adds the specified userName to the specified role.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userName">Name of the userName.</param>
        /// <param name="role">The role.</param>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = true)]
        new public void AddUserToRole(string applicationName, string userName, string role)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
            if (String.IsNullOrEmpty(userName))
                throw new FaultException("userName is null or empty.");
            if (String.IsNullOrEmpty(role))
                throw new FaultException("role is null or empty.");
           
            try
            {
                base.AddUserToRole(applicationName, userName, role);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        /// <summary>
        /// Adds the specified userName to the specified roles.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userName">Name of the userName.</param>
        /// <param name="roles">The roles.</param>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = true)]
        new public void AddUserToRoles(string applicationName, string userName, string[] roles)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
            if (String.IsNullOrEmpty(userName))
                throw new FaultException("userName is null or empty.");
            if (roles == null || roles.Length == 0)
                throw new FaultException("roles is null or empty.");
            
            try
            {
                base.AddUserToRoles(applicationName, userName, roles);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        /// <summary>
        /// Adds a new role to the data source.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="role">The role.</param>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = true)]
        new public void CreateRole(string applicationName, string role)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
            if (String.IsNullOrEmpty(role))
                throw new FaultException("role is null or empty.");
            
            try
            {
                base.CreateRole(applicationName, role);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        /// <summary>
        /// Removes a role from the data source. If throwOnPopulatedRole is true,throws an exception if roleName has one or more members.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="role">The role.</param>
        /// <param name="throwOnPopulatedRole">if set to <c>true</c> [throw on populated role].</param>
        /// <returns></returns>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = true)]
        new public bool DeleteRole(string applicationName, string role, bool throwOnPopulatedRole)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
            if (String.IsNullOrEmpty(role))
                throw new FaultException("role is null or empty.");
           
            try
            {
                return base.DeleteRole(applicationName, role, throwOnPopulatedRole);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return false;
            }
        }

        /// <summary>
        /// Removes all the roles from the data source. If throwOnPopulatedRole is true,throws an exception if any of the roles has one or more members
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="throwOnPopulatedRole">if set to <c>true</c> [throw on populated role].</param>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = true)]
        new public void DeleteAllRoles(string applicationName, bool throwOnPopulatedRole)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
           
            try
            {
                base.DeleteAllRoles(applicationName, throwOnPopulatedRole);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        /// <summary>
        /// Gets a list of all the roles for the applicationName.
        /// </summary>
        /// <param name="applicationName"></param>
        /// <returns></returns>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = false)]
        new public List<Role> GetAllRoles(string applicationName)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
            
            try      
            {
                return base.GetAllRoles(applicationName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return null;
            }
        }

        /// <summary>
        /// Gets a list of the roles that a userName is in.
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = false)]
        new public List<Role> GetRolesByUser(string applicationName, string userName)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
            if (String.IsNullOrEmpty(userName))
                throw new FaultException("userName is null or empty.");
           
            try
            {
                return base.GetRolesByUser(applicationName, userName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return null;
            }
        }

        /// <summary>
        /// Gets a list of userNames in the specified role.
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = false)]
        new public List<User> GetUsersInRole(string applicationName, string roleName)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
            if (String.IsNullOrEmpty(roleName))
                throw new FaultException("roleName is null or empty.");
           
            try
            {
                return base.GetUsersInRole(applicationName, roleName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return null;
            }
        }

        /// <summary>
        /// Removes the specified userName from the specified role.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userName">Name of the userName.</param>
        /// <param name="roleName">Name of the role.</param>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = true)]
        new public void RemoveUserFromRole(string applicationName, string userName, string roleName)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
            if (String.IsNullOrEmpty(userName))
                throw new FaultException("userName is null or empty.");
            if (String.IsNullOrEmpty(roleName))
                throw new FaultException("roleName is null or empty.");
            
            try
            {
                base.RemoveUserFromRole(applicationName, userName, roleName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        /// <summary>
        /// Removes the specified userNames from the specified role.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userNames">The userNames.</param>
        /// <param name="role">The role.</param>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = true)]
        new public void RemoveUsersFromRole(string applicationName, string[] userNames, string role)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
            if (userNames == null || userNames.Length == 0)
                throw new FaultException("userNames is null or empty.");
            if (String.IsNullOrEmpty(role))
                throw new FaultException("role is null or empty.");
            
            try
            {
                base.RemoveUsersFromRole(applicationName, userNames, role);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        /// <summary>
        /// Removes the specified userName from the specified roles.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userName">The userName.</param>
        /// <param name="roles">The roles.</param>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = true)]
        new public void RemoveUserFromRoles(string applicationName, string userName, string[] roles)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
            if (String.IsNullOrEmpty(userName))
                throw new FaultException("userName is null or empty.");
            if (roles == null || roles.Length == 0)
                throw new FaultException("roles is null or empty.");
            
            try
            {
                base.RemoveUserFromRoles(applicationName, userName, roles);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        /// <summary>
        /// Removes the specified userName names from the specified roles.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userNames">The userNames.</param>
        /// <param name="roles">The roles.</param>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = true)]
        new public void RemoveUsersFromRoles(string applicationName, string[] userNames, string[] roles)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
            if (userNames == null || userNames.Length == 0)
                throw new FaultException("userNames is null or empty.");
            if (roles == null || roles.Length == 0)
                throw new FaultException("roles is null or empty.");
            
            try
            {
                base.RemoveUsersFromRoles(applicationName, userNames, roles);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        /// <summary>
        /// Returns true if role management is enabled; otherwise,false
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <returns>
        /// 	<c>true</c> if [is roles enabled] [the specified applicationName]; otherwise, <c>false</c>.
        /// </returns>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = false)]
        new public bool IsRolesEnabled(string applicationName)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
            
            try
            {
                return base.IsRolesEnabled(applicationName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return false;
            }
        }

        /// <summary>
        /// Returns true if the role name already exists in the data source; otherwise,false.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="role">The role.</param>
        /// <returns></returns>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = false)]
        new public bool RoleExists(string applicationName, string role)
        {
            if (String.IsNullOrEmpty(applicationName))
                throw new FaultException("applicationName is null or empty.");
            if (String.IsNullOrEmpty(role))
                throw new FaultException("role is null or empty.");
            
            try
            {
                return base.RoleExists(applicationName, role);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return false;
            }
        }

        #endregion
    }
}
